package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.WmsCkManage;
import com.qf.entity.WmsUser;
import com.qf.service.WmsApplyCkService;
import com.qf.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/apply")
public class WmsApplyCkController {

    @Autowired
    private WmsApplyCkService service;

    /**
     * 查询全部信息并且对这些信息进行分页查询
     *
     * @param map
     * @return
     */
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        // 1 先设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        // 2 正常执行查询
        List<WmsCkManage> wmsCkManage = service.findAll(map);
        // 3 通过查询返回的list创建出分页信息,PageInfo内包含所有分页数据,可以点入源码查看
        PageInfo<WmsCkManage> info = new PageInfo<>(wmsCkManage);
        System.out.println("当前面" + info.getPageNum());
        System.out.println("页面大小" + info.getPageSize());
        System.out.println("总条数" + info.getTotal());
        System.out.println("总页数" + info.getPages());
        System.out.println("数据" + info.getList());
        return R.ok(info);
    }


    //申请出库
    @PostMapping("/applyck")
    public R applyck(@RequestBody WmsCkManage wmsCkManage) {
        boolean isok = service.applyck(wmsCkManage);
        return R.ok(isok);
    }


    /**
     * 查找项目名称，绑定前端下拉框
     *
     * @return
     */
    @GetMapping("/select")
    public R select() {
        List<WmsCkManage> list = service.select();
        return R.ok(list);

    }

    /**
     * 查找分类下拉框
     *
     * @return
     */
    @GetMapping("/selectByClassify")
    public R selectByClassify() {
        List<WmsCkManage> list = service.selectByClassify();
        return R.ok(list);
    }


    /**
     * 查找名称下拉框
     *
     * @return
     */
    @GetMapping("/selectByName")
    public R selectByName() {
        List<WmsCkManage> list = service.selectByName();
        return R.ok(list);
    }

    /**
     * 查找品牌下拉框
     *
     * @return
     */
    @GetMapping("/selectByBrand")
    public R selectByBrand() {
        List<WmsCkManage> list = service.selectByBrand();
        return R.ok(list);
    }

    /**
     * 出库
     */
    @PostMapping("/ck")
    public R ck(@RequestBody WmsCkManage wmsCkManage, HttpSession session) {
        WmsUser info = (WmsUser) session.getAttribute("userInfo");
        //System.out.println(info);
        //String username = info.getUsername();
        int inventoryQuantity = wmsCkManage.getInventoryQuantity();//库存数量
        int qutboundQuantity = wmsCkManage.getQutboundQuantity();//出库数量
        Integer id = wmsCkManage.getId();
        int rul = inventoryQuantity - qutboundQuantity;
//          System.out.println(rul);
         service.ck(id ,rul);
         return R.ok();
    }
}
